package bounjour.ui;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import bounjour.User;

import java.io.*;


/**
 * Servlet implementation class Login
 */
public class Login extends BaseServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		map.put("title", "Login");
		map.put("message", "");
		process(request, response, "login.ftl", false);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		User user = User.login(request.getParameter("userEmail"), request.getParameter("userPassword"));
		if (user != null) {
			HttpSession session = request.getSession();
			session.setAttribute("user", user);
			response.sendRedirect("Home");
		}
		else
		{
			map.put("title", "Login");
			map.put("message", "<p class=\"failureNotification\">Incorrect Username or Password!</p>");
			process(request, response, "login.ftl", false);
		}		
	}
	
	

}
